import Vue from 'vue'
import VueRouter from 'vue-router'
import manage from '../views/manage/manage.vue'
import store from "@/store/store";
import Cookies from "js-cookie";

Vue.use(VueRouter)

const routes = [

    //登录
  {
    path: '/login',
    name: '登录界面',
    component: () => import(/* webpackChunkName: "about" */ '../views/Login.vue')
  },
    //管理员主页
  {
    path: '/manage/',
    name: '管理员界面',
    component: manage,
    redirect: '/manage/home',
    children: [
      {
        path: 'home',
        name: 'home',
        component: () => import('../views/HomeView')
      },
      {
        path: 'user',
        name: '读者信息',
        component: () => import(/* webpackChunkName: "about" */ '../views/user/User.vue')
      },
      {
        path: 'admin',
        name: '管理员信息',
        component: () => import(/* webpackChunkName: "about" */ '../views/admin/Admin.vue')
      },
      {
        path: 'category',
        name: '图书分类',
        component: () => import(/* webpackChunkName: "about" */ '../views/category/Category.vue')
      },
      {
        path: 'book',
        name: '图书列表',
        component: () => import(/* webpackChunkName: "about" */ '../views/book/Book.vue')
      },
      {
        path: 'newspaper',
        name: '期刊列表',
        component: () => import(/* webpackChunkName: "about" */ '../views/book/Newspaper.vue')
      },
      {
        path: 'borrow',
        name: '借阅管理',
        component: () => import(/* webpackChunkName: "about" */ '../views/borrow/Borrow.vue')
      },
      {
        path: 'returnBook',
        name: '还书列表',
        component: () => import(/* webpackChunkName: "about" */ '../views/returnBook/returnBook')
      },
      {
        path: 'Activity',
        name: '活动列表',
        component: () => import(/* webpackChunkName: "about" */ '../views/returnBook/Activity')
      },
    ]
  },

  {
    path: '*',
    component: () => import('../views/404')
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {
  localStorage.setItem("currentPathName", to.name)
  store.commit("setPath")
  next()
})

router.beforeEach((to,from,next) => {
  if(to.path === '/login') next()
  const admin = Cookies.get("admin")
  if (!admin && to.path !== "/login") return next ("/login") //强制退到登录页面
  next()
})

export default router
